#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_adc.h"
#include "hi_errno.h"
#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO 25
#define LED_TEST_GPIO 10 //10 for hispark_pegasus
#define TASK_DELAY_TIME 200000
#define F1 9 //5 for hispark_pegasus
#define F2 5
int LED_INTERVAL_TIME_US = 300000;
enum LedState {
LED_ON = 0,
LED_OFF,
LED_SPARK,
};
enum LedSpeed {
FAST = 0,
MIDDLE,
SLOW,
};
enum LedState g_LedState = LED_SPARK;
enum LedSpeed g_Ledspeed = FAST;



static void F1_Pressed (char *arg)
{
(void) arg;
enum LedState nextState = LED_SPARK;
printf ("F1 Pressed\n");
// 切换状态逻辑...
switch (g_LedState) {
case LED_ON:
nextState = LED_OFF;
break;
case LED_OFF:
nextState = LED_SPARK;
break;
case LED_SPARK:
nextState = LED_ON;
break;
default:
break;
}
g_LedState = nextState;
usleep (TASK_DELAY_TIME); // 200ms sleep
}


static void F2_Pressed (char *arg)
{
(void) arg;
enum LedSpeed nextSpeed = MIDDLE;
printf ("F2 Pressed\n");
// 切换状态逻辑...
switch (g_Ledspeed) {
case FAST:
nextSpeed = SLOW;
break;
case SLOW:
nextSpeed = MIDDLE;
break;
case MIDDLE:
nextSpeed = FAST;
break;
default:
break;
}
g_Ledspeed = nextSpeed;
usleep (TASK_DELAY_TIME); // 200ms sleep
}

static void ButtonledInit(void)
{
IoTGpioInit(LED_TEST_GPIO);
hi_io_set_func(LED_TEST_GPIO, HI_IO_FUNC_GPIO_10_GPIO);
IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
hi_gpio_init (); // GPIO 初始化
hi_io_set_pull (F1, HI_IO_PULL_UP); // 设置 GPIO 上拉
hi_io_set_func (F1, HI_IO_FUNC_GPIO_9_GPIO); // 设置 IO 为 GPIO 功能
hi_gpio_set_dir (F1, HI_GPIO_DIR_IN); // 设置 GPIO 为输入模式
hi_gpio_register_isr_function (F1, // KEY 按键引脚
HI_INT_TYPE_EDGE, // 下降沿检测
HI_GPIO_EDGE_FALL_LEVEL_LOW, // 低电平时触发
&F1_Pressed, // 触发后调用的回调函数
NULL
); // 回调函数的传参值
hi_io_set_pull (F2, HI_IO_PULL_UP); // 设置 GPIO 上拉
hi_io_set_func (F2, HI_IO_FUNC_GPIO_5_GPIO); // 设置 IO 为 GPIO 功能
hi_gpio_set_dir (F2, HI_GPIO_DIR_IN); // 设置 GPIO 为输入模式
hi_gpio_register_isr_function (F2, // KEY 按键引脚
HI_INT_TYPE_EDGE, // 下降沿检测
HI_GPIO_EDGE_FALL_LEVEL_LOW, // 低电平时触发
&F2_Pressed, // 触发后调用的回调函数
NULL
); // 回调函数的传参值
}





static void *LedTask(const char *arg)
{
(void)arg;
while (1) {
switch (g_LedState) {
case LED_ON:
IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
usleep(LED_INTERVAL_TIME_US);
break;
case LED_OFF:
IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
usleep(LED_INTERVAL_TIME_US);
break;
case LED_SPARK:
IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
usleep(LED_INTERVAL_TIME_US);
IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
usleep(LED_INTERVAL_TIME_US);
break;
default:
usleep(LED_INTERVAL_TIME_US);
break;
}
switch (g_Ledspeed) {
case FAST:
LED_INTERVAL_TIME_US = 300000;
break;
case MIDDLE:
LED_INTERVAL_TIME_US = 600000;
break;
case SLOW:
LED_INTERVAL_TIME_US = 900000;
break;
}
}
return NULL;
}





static void LedExampleEntry(void)
{
osThreadAttr_t attr;
ButtonledInit();
attr.name = "LedTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = LED_TASK_STACK_SIZE;
attr.priority = LED_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
printf("[LedExample] Falied to create LedTask!\n");
}
}

SYS_RUN(LedExampleEntry);
